package cz.masa.FotoCAT;

import java.awt.event.*;
import javax.swing.*;
import java.sql.*;

class StorePolozka extends treePolozka{
    private fcaStorePlace storePlace;

    public StorePolozka(fcaStorePlace sp){
        createPopup();
        storePlace = sp;
        this.name = storePlace.getName();
    }    
    
    public StorePolozka(int id){
        createPopup();
        storePlace = new fcaStorePlace(id);
        this.name = storePlace.getName();
    }

    public void infoDlg(){
        JOptionPane.showInternalMessageDialog(FotoCAT.desktop, getInfo(INFO_FULL));
    }    

    public String getInfo(int style){
        String ret = "";
        if(style == INFO_UTREE){
            ret += "Name: " + storePlace.getName() + "\n";
            ret += "Type: " + storePlace.getTypeStr() + "\n";
            ret += "Path: " + storePlace.getPath() + "\n";
            ret += "Directories: " + storePlace.getDirCount();
        }
        if(style == INFO_FULL){
            ret += "Name: " + storePlace.getName() + "\n";
            ret += "Type: " + storePlace.getTypeStr() + "\n";
            ret += "Path: " + storePlace.getPath() + "\n";
            ret += "Data amount: " + FotoCATutil.getSizeString(storePlace.getDataAmount()) + "\n";
            ret += "Directories: " + storePlace.getDirCount();
        }
        return ret;
    }
    
    public void Delete(){
        int dircnt = storePlace.getDirCount();
        if(dircnt > 0 || dircnt == -1){
            JOptionPane.showInternalMessageDialog(FotoCAT.desktop, "This store place include some directories. It can't be deleted.", "Warning", JOptionPane.WARNING_MESSAGE);
            return;
        }
        storePlace.dbDelete();
        FotoCAT.rereadDirPan();
    }      
    
    private void createPopup(){
        JMenuItem menuItem;
        popup = new JPopupMenu();

        menuItem = new JMenuItem("Informations");
        menuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                infoDlg();
            }
        });
        popup.add(menuItem);

        menuItem = new JMenuItem("Edit");
        menuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                StoreEdit.openOnDesktop(storePlace);
            }
        });
        popup.add(menuItem);
        
        menuItem = new JMenuItem("Delete");
        menuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Delete();
            }
        });
        popup.add(menuItem);
    }
}

